import {simpleGet} from "../utils/request";

const app = getApp()


const defaultList = {
    page:1,
    items:[],
    more:'loading',
    limit:10,
    url:'',
    where:{},
}

export default Behavior({
    data:{
        list:{...defaultList}
    },

    methods:{
        setList(page){

            if(page == 1){	//　如果传入的第一页，则重置列表信息

                this.setData({
                    [`list.page`]:1,
                    [`list.items`]:[],
                    [`list.more`]:'loading',
                })
            }

            const {list} = this.data

            if(list.more == 'noMore'){	// 全部加载完成
                return;
            }
            // 加载中
            list.more = 'loading';
            const _params = {
                limit: list.limit,
                page: list.page,
            }


            simpleGet(list.url, {..._params, ...list.where}).then((res) => {

                const items = res.items

                list.items = list.items.concat(items)
                list.page++
                if(items.length < list.limit){
                    list.more = 'noMore';
                }else{
                    list.more = 'more'
                }

                this.setData({
                    list,
                })
            })
        }
    }
})
